﻿using System;
using System.Collections.ObjectModel;
using Com.PhilChuang.Mobile.Apps.LocationSender.Model.Data;
using Com.PhilChuang.Pmvp.Ui.Views.Impl.ViewModels;

namespace Com.PhilChuang.Mobile.Apps.LocationSender.Ui.Views.Impl.ViewModels
{
	public class SelectFormatPageViewModel : AbstractLocationSenderAppViewModel
	{
		private GeoCoordinateFormat myFormat;
		public GeoCoordinateFormat Format
		{
			get { return myFormat; }
			set
			{
				myFormat = value;
				RaisePropertyChanged (() => Format);
			}
		}

		private ObservableCollection<String> myRecipients;
		public ObservableCollection<String> Recipients
		{
			get { return myRecipients; }
			set
			{
				myRecipients = value;
				RaisePropertyChanged (() => Recipients);
			}
		}

		private ObservableCollection<FormatAndResult> myFormatsAndResults;
		public ObservableCollection<FormatAndResult> FormatsAndResults
		{
			get { return myFormatsAndResults; }
			set
			{
				myFormatsAndResults = value;
				RaisePropertyChanged (() => FormatsAndResults);
			}
		}

		public SelectFormatPageViewModel ()
		{
			myRecipients = new ObservableCollection<string> ();
			myFormatsAndResults = new ObservableCollection<FormatAndResult> ();
			PropertyChanged += OnPropertyChanged;

			foreach (var format in Com.PhilChuang.Utils.Mobile.Extensions.EnumGetValues<GeoCoordinateFormat> ())
			{
				if (format == GeoCoordinateFormat.None) continue;
				myFormatsAndResults.Add (new FormatAndResult { Format = format });
			}
		}

		protected override void OnPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			base.OnPropertyChanged (sender, e);
			
			if (e.PropertyName == INPCHelper.GetPropertyName (() => CurrentPosition))
			{
				foreach (var far in myFormatsAndResults)
					far.Location = CurrentPosition;
			}
		}
	}

	public class FormatAndResult : BaseINPC
	{
		private GeoCoordinateFormat myFormat;
		public GeoCoordinateFormat Format
		{
			get { return myFormat; }
			set
			{
				myFormat = value;
				RaisePropertyChanged (() => Format);
			}
		}

		private GeoCoordinate myLocation;
		public GeoCoordinate Location
		{
			get { return myLocation; }
			set
			{
				myLocation = value;
				RaisePropertyChanged (() => FormatResult);
			}
		}

		public String FormatResult
		{
			get { return myLocation != null ? myLocation.ToString (myFormat) : String.Empty; }
		}
	}
}
